import {t} from 'elysia'
import {ElysiaApp} from '..'

export default (app: ElysiaApp) =>
  app
    // 获取用户分页列表
    .get(
      '/page/list',
      async ({prisma, query}) => {
        const {page, pageSize = 10, url, ip} = query
        const _res = await prisma.$transaction([
          prisma.reqLog.findMany({
            skip: (page - 1) * pageSize,
            take: pageSize,
            where: {AND: {ip: {contains: ip}, url: {contains: url}}},
            include: {
              user: {select: {username: true}},
            },
            orderBy: [{createTime: 'desc'}, {id: 'desc'}],
          }),
          prisma.reqLog.count({
            where: {AND: {ip: {contains: ip}, url: {contains: url}}},
          }),
        ])
        return {list: _res[0], total: _res[1]}
      },
      {
        query: t.Object({
          page: t.Numeric(),
          pageSize: t.Optional(t.Numeric()),
          url: t.Optional(t.String()),
          ip: t.Optional(t.String()),
        }),
      },
    )
